package com.sgb.kjwl.viewmodel.manage;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JingYingSearchModel extends AndroidViewModel {

    public JingYingSearchModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<List<String>> historyRecordLD = new MutableLiveData<>();
    public MutableLiveData<Boolean> isSuccessFllowLD = new MutableLiveData<>();
    public MutableLiveData<String> msgLD = new MutableLiveData<>();
    public MutableLiveData<List<TheEnterpriseOfEntity>> companyListLD = new MutableLiveData<>();

    private Map<String, Object> map = new HashMap<>();

    /**
     * 获取本地搜索记录
     */
    public void getSearchHistoryListData() {
        //取出本地存的搜索记录
        String historyWords =MkvUtils.get().getString(MkvUtils.CommomData, CacheMarker.JY_SEARCH_HISTORY_RECORD, "");
        if (!TextUtils.isEmpty(historyWords)) {
            String[] arr = historyWords.split(","); //字符串转数组,  用,分割
            List<String> resultList = new ArrayList<>(arr.length);
            for (String s : arr) {
                resultList.add(s);
            }
            historyRecordLD.postValue(resultList);
        } else {
            historyRecordLD.postValue(new ArrayList<>());
        }
    }


    /**
     * 将搜索词汇记录在本地
     *
     * @param newWords
     */
    public void recordsLocal(String newWords) {
        String historyWords =MkvUtils.get().getString(MkvUtils.CommomData, CacheMarker.JY_SEARCH_HISTORY_RECORD, "");
        StringBuffer sb = new StringBuffer();
        sb.append(newWords);
        if (!TextUtils.isEmpty(historyWords)) {
            String[] arr = historyWords.split(","); //字符串转数组,  用,分割
            for (int i = 0; i < arr.length; i++) {
                if (i < 9) {//最多只记录10条
                    if (!newWords.equals(arr[i])) {
                        sb.append("," + arr[i]);
                    }
                }
            }
        }
        MkvUtils.get().putString(MkvUtils.CommomData, CacheMarker.JY_SEARCH_HISTORY_RECORD, sb.toString());
    }

    //获取列表数据
    public void getComList(Map<String, Object> map, Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getComList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Response<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    companyListLD.postValue(response.body().getData().getList());
                } else {
                    MToast.showLongToast(context, "数据异常");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TheEnterpriseOfEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void follow(String shopId) {
        APIManager.getInstance().getGoodsAPI().userFocusCompany(shopId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    isSuccessFllowLD.postValue(true);
                } else {
                    DialogHelper.dismissProgressDialog();
                    msgLD.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
